package com.vst.test.generate.structure.springmvc.plugin.builder;

import java.util.Iterator;

import javax.xml.bind.JAXBException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.vst.test.generate.structure.springmvc.plugin.analizador.AnalizadorEntidadesDominio;
import com.vst.test.generate.structure.springmvc.plugin.analizador.AnalizadorPaqueteDAO;
import com.vst.test.generate.structure.springmvc.plugin.analizador.AnalizarrecursosInProject;

public class ToggleNatureAction implements IObjectActionDelegate {

	private ISelection selection;

	@SuppressWarnings("rawtypes")
	public void run(IAction action) {
		System.out.println("ToggleNatureAction run");
		if (selection instanceof IStructuredSelection) {
			System.out.println("ToggleNatureAction run selection is IStructuredSelection");
			for (Iterator it = ((IStructuredSelection) selection).iterator(); it.hasNext();) {
				Object element = it.next();
				System.out.println("ToggleNatureAction run Object element:"+element);
				IProject project = null;
				if (element instanceof IProject) {
					project = (IProject) element;
					System.out.println("ToggleNatureAction run IProject:"+project);				
					if (project != null) {
						System.out.println("ToggleNatureAction antes de toggleNature : "+project);
//						toggleNature(project);
					}					
					AnalizarrecursosInProject analizador = new AnalizarrecursosInProject(project); 
					try {
						analizador.findRecursoXMLinProject();
						System.out.println("isResourceValid:"+analizador.isResourceValid());
						if(analizador.isResourceValid()){
							analizador.analizarPackageUtil();
							System.out.println("isExistePackageUtil:"+analizador.isExistePackageUtil());
							if(!analizador.isExistePackageUtil()){
								analizador.generarPackageAndClasses();
							}else{
								analizador.generarClasesFaltantes();
							}
						}	
						if(analizador.existeSourceRequerido()){
							AnalizadorEntidadesDominio analizadorEntidadesDominio = new AnalizadorEntidadesDominio(analizador);
							analizadorEntidadesDominio.scanDominio();
							if(analizadorEntidadesDominio.existenEntidades()){
								AnalizadorPaqueteDAO analizadorPaqueteDAO = new AnalizadorPaqueteDAO(analizador,analizadorEntidadesDominio);
								analizadorPaqueteDAO.scanPackageExists();
								if(!analizadorPaqueteDAO.existePackageDAO()){
									analizadorPaqueteDAO.createPackageAndClasesDAOGeneral();
								}else{
									analizadorPaqueteDAO.updatePackageAndClasesDAOForEntidad();
								}
								analizadorPaqueteDAO.generarMetodosBasicos();
							}							
						}						
					} catch (JAXBException e) {
						e.printStackTrace();
					} catch (JavaModelException e) {						
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 					
				} 
//					else if (element instanceof IAdaptable) {
//					project = (IProject) ((IAdaptable) element)
//							.getAdapter(IProject.class);
//					System.out.println("ToggleNatureAction run IAdaptable:"+project);
//					System.out.println("ToggleNatureAction run IAdaptable:"+project.getName());
//					System.out.println("ToggleNatureAction run IAdaptable:"+project.getLocationURI().getPath());
//				}
				
			}
		}
	}


	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
		System.out.println("ToggleNatureAction selectionChanged");
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		System.out.println("setActivePart");
	}

//	private void toggleNature(IProject project) {
//		try {
//			System.out.println("toggleNature project:"+project);
//			IProjectDescription description = project.getDescription();
//			System.out.println("toggleNature description:"+description.getName());
//			String[] natures = description.getNatureIds();
//			for (int i = 0; i < natures.length; ++i) {
//				if (EstructureNatureClassBuilder.NATURE_ID.equals(natures[i])) {
//					// Remove the nature
//					String[] newNatures = new String[natures.length - 1];
//					System.arraycopy(natures, 0, newNatures, 0, i);
//					System.arraycopy(natures, i + 1, newNatures, i,	natures.length - i - 1);
//					description.setNatureIds(newNatures);
//					project.setDescription(description, null);
//					return;
//				}
//			}
//
//			// Add the nature
//			String[] newNatures = new String[natures.length + 1];
//			System.arraycopy(natures, 0, newNatures, 0, natures.length);
//			newNatures[natures.length] = EstructureNatureClassBuilder.NATURE_ID;
//			description.setNatureIds(newNatures);
//			project.setDescription(description, null);
//		} catch (CoreException e) {
//		}
//	}

}
